package com.eam.asset.file.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eam.asset.file.domain.AssetPlace;
import com.eam.asset.file.dto.AssetPlace.AssetPlaceDto;
import com.eam.asset.file.dto.AssetPlace.AssetPlaceAddDto;
import com.eam.asset.file.mapper.AssetPlaceMapper;
import com.eam.asset.file.utils.AssetEncode;
import com.eam.common.constant.Constants;
import com.eam.common.core.redis.SRTemplateCache;
import com.eam.common.utils.DateUtils;
import com.eam.common.utils.SecurityUtils;
import com.eam.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eam.asset.file.service.IAssetPlaceService;

import static com.eam.asset.file.Enum.AssetEncodeEnum.ASSETPLACE;

/**
 * 场地资产档案Service业务层处理
 *
 * @author tzf
 * @date 2023-10-31
 */
@Service
public class AssetPlaceServiceImpl extends ServiceImpl<AssetPlaceMapper, AssetPlace> implements IAssetPlaceService {
    private final SRTemplateCache SRTemplateCache;

    //构造器注入
    @Autowired
    public AssetPlaceServiceImpl(SRTemplateCache srTemplateCache) {
        SRTemplateCache = srTemplateCache;
    }

    /**
     * 查询场地资产档案
     *
     * @param placeId 场地资产档案主键
     * @return 场地资产档案
     */
    @Override
    public AssetPlace selectAssetPlaceByPlaceId(String placeId) {
        //如果存在key,从redis中取值
        if (SRTemplateCache.hasKey(placeId)) {
            return SRTemplateCache.get(placeId, AssetPlaceDto.class);
        }
        AssetPlace assetPlace = baseMapper.selectById(placeId);
        AssetPlaceDto assetPlaceDto = new AssetPlaceDto();
        BeanUtils.copyProperties(assetPlace, assetPlaceDto);
        //存进redis
        SRTemplateCache.set(placeId, JSONUtil.toJsonStr(assetPlaceDto));
        return assetPlaceDto;
    }

    /**
     * 查询场地资产档案列表
     *
     * @param assetPlace 场地资产档案
     * @return 场地资产档案
     */
    @Override
    public List<AssetPlace> selectAssetPlaceList(AssetPlace assetPlace) {
        //todo 后续做redis缓存,考虑数据一致性,如增删改操作需要考虑
        AssetPlaceDto assetPlaceDto = new AssetPlaceDto();
        //深拷贝
        BeanUtils.copyProperties(assetPlace, assetPlaceDto);
        return baseMapper.selectAssetPlaceList(assetPlaceDto);
    }

    /**
     * 新增场地资产档案
     *
     * @param assetPlace 场地资产档案
     * @return 结果
     */
    @Override
    public int insertAssetPlace(AssetPlace assetPlace) {
        //创建dto对象,深拷贝属性值
        AssetPlaceAddDto assetPlaceAddDto = new AssetPlaceAddDto();
        BeanUtils.copyProperties(assetPlace, assetPlaceAddDto);
        //设置基本的信息
        assetPlaceAddDto.setCreateBy(AssetEncode.IfAbsent(SecurityUtils.getUserId()));
        assetPlaceAddDto.setCreateTime(DateUtils.getNowDate());
        assetPlaceAddDto.setStatus(Constants.ZERO);
        //设置资产档案编号,一级资产分类编码+二级资产分类编码+三级资产名称编码加编号
        StringBuffer encode = AssetEncode.getEncode(ASSETPLACE.getType()).
                append(AssetEncode.getEncode(assetPlaceAddDto.getPlaceCategory())).
                append(AssetEncode.getEncodeByName(assetPlaceAddDto.getPlaceName())).
                append(DateUtils.dateTime());
        assetPlaceAddDto.setPlaceCode(encode.toString());
        return baseMapper.insert(assetPlace);
    }

    /**
     * 修改场地资产档案
     *
     * @param assetPlace 场地资产档案
     * @return 结果
     */
    @Override
    public int updateAssetPlace(AssetPlace assetPlace) {
        assetPlace.setUpdateTime(DateUtils.getNowDate());
        assetPlace.setUpdateBy(AssetEncode.IfAbsent(SecurityUtils.getUserId()));
        AssetPlaceDto assetPlaceDto = new AssetPlaceDto();
        BeanUtils.copyProperties(assetPlace, assetPlaceDto);
        return baseMapper.updateAssetPlace(assetPlaceDto);
    }

    /**
     * 批量删除场地资产档案
     *
     * @param placeIds 需要删除的场地资产档案主键
     * @return 结果
     */
    @Override
    public int deleteAssetPlaceByPlaceIds(String[] placeIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(placeIds));
    }

    /**
     * 删除场地资产档案信息
     *
     * @param placeId 场地资产档案主键
     * @return 结果
     */
    @Override
    public int deleteAssetPlaceByPlaceId(String placeId) {
        return baseMapper.deleteById(placeId);
    }
}
